(function (G) {
  var models = {}, api = '/api/model/unit';
  G.vue({
    data: {
      tree: [],
      title:'',
      nickName:'',
      userNickName:''
    },
    methods: {
      getItem: function (event, next) {
        var index = event.currentTarget.dataset.index.toString();
        index = index ? index.split(',') : [];
        var tree = this.getData('tree'), item = '',parent = '';
        for (var i = 0; i < index.length; i++) {
          if (i === 0) {
            item = tree[index[0]];
          } else {
            parent = item;
            item = item.children[index[i]];
          }
        }
        next.call(this, item, tree, index,parent);
      },
      change: function (event) {
        this.getItem(event, function (item, tree) {
          item.opened = !item.opened;
          this.setData({
            tree: tree
          });
        });
      },
      delet: function (event) {
        var _this = this;
        this.getItem(event, function (item, tree, index,parent) {
          G.confirmx('确定删除?', function () {
            G.ajax(api + '?id=' + item.id, function (ret) {
              if (ret.errorMsg) {
                G.showToast('删除失败!!' + ret.errorMsg);
              } else {
                if(parent){
                  parent.children.splice(index[index.length - 1], 1);
                }else{
                  tree.splice(index[0], 1);
                }
                _this.setData({
                  tree: tree
                });
              }
            }, {
                meth: 'DELETE'
              })
          })
        })
      },
      users:function(event){
        this.getItem(event, function (item) {
          this.$go('/pages/unit/user/user?name=' + item.name + '&unitId=' + item.unitId);
        })
      },
      edit: function (event) {
        var _this = this;
        this.getItem(event, function (item) {
          G.Storage.set('modeleditquery', {
            values: item,
            title: '修改 ' + item.name,
            meth: 'PUT',
            refresh: this.getList,
            models: models,
            url: api
          });
          this.$go('/pages/model/edit/edit');
        })
      },
      add: function (event) {
        var _this = this;
        this.getItem(event, function (item) {
          G.Storage.set('modeleditquery', {
            values: {},
            title: (item ? item.name : '') + ' 新增子' + _this.getData('nickName'),
            meth: 'POST',
            refresh: this.getList,
            models: models,
            url: api,
            submit: function (data) {
              if (item) {
                data.parentUnitId = item.unitId;
                data.level = parseInt(item.level) + 1;
                //data.unitId = item.unitId || (item.unitId + '_' + G.guid());
                data.unitId = item.unitId + '_' + G.guid()
              }else{
                data.level = 1;
              }
              return data;
            }
          });
          this.$go('/pages/model/edit/edit');
        })
      },
      getList: function () {
        var _this = this,userInfo = G.Storage.get('userInfo');
        G.ajax(api, function (ret) {
          if (!ret.errorMsg) {
            var tree = {
              '0': {
                children: []
              }
            };
            for (var i = 0; i < ret.length; i++) {
              if(ret[i].unitId == userInfo.unitId) ret[i].parentUnitId = '0';
              ret[i].parentUnitId = ret[i].parentUnitId || '0';
              ret[i].opened = true;
              ret[i].children = [];
              tree[ret[i].unitId] = ret[i];
              if (tree[ret[i].parentUnitId]) tree[ret[i].parentUnitId].children.push(ret[i])
            }
            _this.setData({
              tree: tree['0'].children
            })
          }
        },{
          data:{
            sql:'classifySort'
          }
        })
      }
    },
    mounted: function (query) {
      query = {
        modelName:'unit',
        nickName:G.Storage.get('nickNames').unit || '单位'
      }
      var title = query.nickName;
      var _this = this;
      this.getList();
      G.ajax('/model/unit', function (ret) {
        _this.setData({
          title:title,
          nickName:query.nickName,
          userNickName: G.Storage.get('nickNames').user || '用户'
        });
        models = ret;
      }, {
          storageName: 'unitModel'
        })
    }
  });
})(wx.$)